package com.ecoolex.discount.sales.core.manager;

import com.ecoolex.discount.sales.core.dao.DiscountBrandDAO;
import com.ecoolex.discount.sales.entity.DiscountBrand;
import com.ecoolex.discount.sales.entity.DiscountBrandExample;
import com.ecoolex.discount.sales.entity.DiscountBrandExample.Criteria;
import com.ecoolex.discount.sales.entity.DiscountBrandWithBLOBs;
import com.ecoolex.discount.sales.entity.DiscountBrandWithBLOBs.Column;
import com.ecoolex.discount.sales.entity.DiscountClassify;
import com.ecoolex.discount.sales.entity.DiscountProductWithBLOBs;
import com.ecoolex.discount.sales.enums.DiscountBrandStatus;
import com.ecoolex.discount.sales.enums.DiscountClassifyRegionType;
import com.ecoolex.discount.sales.pojo.AddDiscountBrandClassifyDTO;
import com.ecoolex.discount.sales.pojo.QueryDiscountBrandDTO;
import com.ecoolex.discount.sales.pojo.QueryMarketingClassifyBrandDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.exception.BizException;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.ecoolex.discount.sales.entity.DiscountBrand.NOT_DELETED;

import java.time.Instant;
import java.util.Date;
import java.util.List;

@Service
public class DiscountBrandManager {
	
    @Autowired
    private DiscountBrandDAO dao;
    
    @Autowired
    private DiscountBrandClassifyManager discountBrandClassifyManager;
    
    @Autowired
    private DiscountProductManager discountProductManager;
    
    @Autowired
    private DiscountClassifyManager discountClassifyManager;

    public void verify(DiscountBrandWithBLOBs record,List<AddDiscountBrandClassifyDTO> list) {
    	BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getName()),ResultCode.VERIFY.build(10), "品牌名称为空");
    	BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getLogo()),ResultCode.VERIFY.build(11), "品牌Logo为空");
    	BizAssert.isTrue(Check.notNullAndGtZero(record.getBrandMode()),ResultCode.VERIFY.build(12), "品牌类型为空");
    	BizAssert.isTrue(Check.notNull(record.getIsNew()),ResultCode.VERIFY.build(13), "品牌是否上新状态为空");
    	BizAssert.isTrue(Check.notNull(record.getWeight()),ResultCode.VERIFY.build(14), "品牌权重为空");
    	BizAssert.isTrue(Check.notNullOrEmpty(list),ResultCode.VERIFY.build(15), "至少需要一个分类");
//    	int internal = 0;
//    	int foregin = 0;
//    	for (AddDiscountBrandClassifyDTO dto : list) {
//    		DiscountClassify discountClassify = discountClassifyManager.getDiscountClassify(dto.getClassifyId());
//			if(DiscountClassifyRegionType.INTERNAL.real().equals(discountClassify.getRegionType())) {
//				internal++;
//			}
//			if(DiscountClassifyRegionType.FOREIGN.real().equals(discountClassify.getRegionType())) {
//				foregin++;
//			}
//		}
//    	BizAssert.isTrue(Check.isLessThan(internal, 2),ResultCode.VERIFY.build(16), "国内分类数量超过1个");
//    	BizAssert.isTrue(Check.isLessThan(foregin, 2),ResultCode.VERIFY.build(17), "国外分类数量超过1个");
    }
    
    @Transactional
	public DiscountBrandWithBLOBs addDiscountBrand(DiscountBrandWithBLOBs record, List<AddDiscountBrandClassifyDTO> list, String createUser){
		verify(record,list);
		
		record.setStatus(DiscountBrandStatus.OFFLINE.real());
		record.setDeleted(DiscountBrandWithBLOBs.NOT_DELETED);
		record.setCreateUser(createUser);
		record.setCreateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.insert(record);
		//绑定分类
		discountBrandClassifyManager.update(record.getBrandId(), list, createUser);
		return record;
	}

    @Transactional
	public DiscountBrandWithBLOBs editDiscountBrand(DiscountBrandWithBLOBs record, List<AddDiscountBrandClassifyDTO> list, String updateUser){
    	verify(record,list);
    	record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.excludes(Column.brandMode,Column.status,Column.deleted,Column.createUser,Column.createTime));
		//修改所属分类
		discountBrandClassifyManager.update(record.getBrandId(), list, updateUser);
		return record;
	}

    @Transactional
	public void deleteDiscountBrand(DiscountBrandWithBLOBs record, String updateUser){
		//检查关联的折扣产品
		List<DiscountProductWithBLOBs> list = discountProductManager.queryByBrandId(record.getBrandId());
		BizAssert.isTrue(Check.isNullOrEmpty(list), ResultCode.VERIFY.build(10),"存在关联的折扣产品");
		record.setDeleted(DiscountBrandWithBLOBs.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record,Column.deleted,Column.updateUser,Column.updateTime);
		//删除与该品牌关联的分类关系
		discountBrandClassifyManager.deleteByBrandId(record.getBrandId(), updateUser);
	}
	
	public void onlineDiscountBrand(DiscountBrandWithBLOBs record, String updateUser) throws BizException {
		record.setStatus(DiscountBrandStatus.ONLINE.real());
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record,Column.status,Column.updateUser,Column.updateTime);
	}

	public void offlineDiscountBrand(DiscountBrandWithBLOBs record, String updateUser) throws BizException {
		record.setStatus(DiscountBrandStatus.OFFLINE.real());
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record,Column.status,Column.updateUser,Column.updateTime);
	}

	public DiscountBrandWithBLOBs getDiscountBrand(Integer brandId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(brandId, DiscountBrandWithBLOBs.NOT_DELETED);
	}
	
	public DiscountBrandWithBLOBs getDiscountBrandForH5(Integer brandId) {
		DiscountBrandExample example = new DiscountBrandExample();
		Criteria criteria = example.createCriteria();
		criteria.andBrandIdEqualTo(brandId).andStatusEqualTo(DiscountBrandStatus.ONLINE.real()).andDeletedEqualTo(DiscountBrandWithBLOBs.NOT_DELETED);
		return dao.selectOneByExampleWithBLOBs(example);
	}

	public Pagination<DiscountBrandWithBLOBs> queryDiscountBrand(QueryDiscountBrandDTO query,Sorting[] sorts, Page page) {
		return Pagination.create(page, 
			dao.count(query),
			() -> dao.query(query,Sortings.builder(sorts).toSql(),page));
	}
	
	public DiscountBrand getByName(String name) {
        DiscountBrandExample example = new DiscountBrandExample();
        example.createCriteria()
                .andDeletedEqualTo(NOT_DELETED)
                .andNameEqualTo(name);
        return dao.selectOneByExample(example);
    }
	
	public List<DiscountBrandWithBLOBs> listDiscountBrandForH5(QueryMarketingClassifyBrandDTO query,Integer size){
		return dao.listForH5(query,DiscountBrandStatus.ONLINE.real(),size);
	}
}